package com.byx.chatclient.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.byx.chatclient.R;
import com.byx.chatclient.bean.GroupChatInfo;
import com.byx.chatclient.bean.GroupInfo;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * @ClassName GroupListAdapter
 * @Description 群聊展示适配器
 * @Author Superb
 * @Date 2021/5/5
 * @Version 1.0
 */
public class GroupListAdapter extends BaseExpandableListAdapter {
    private static final int BG_PUT_AWAY_TRIANGLE_RES_ID = R.mipmap.ic_triangle_gray_48;
    private static final int BG_EXPAND_TRIANGLE_RES_ID = R.mipmap.ic_triangle_expand_gray_48;
    public static final int LAYOUT_RES_GROUP_ID = R.layout.fragment_contact_group_item_group_view;
    public static final int LAYOUT_RES_CHILD_ID = R.layout.fragment_contact_group_item_child_view;

    private final Context mContext;
    private List<GroupInfo> mGroupList;

    public GroupListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public GroupListAdapter(Context mContext, List<GroupInfo> mGroupList) {
        this.mContext = mContext;
        this.mGroupList = mGroupList;
    }

    public void setGroupList(List<GroupInfo> groupList) {
        this.mGroupList = groupList;
    }

    @Override
    public int getGroupCount() {
        return mGroupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mGroupList.get(groupPosition).getChildInfoList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mGroupList.get(groupPosition).getChildInfoList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return ((GroupChatInfo) mGroupList.get(groupPosition).getChildInfoList().get(childPosition)).getGroupId();
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupInfo groupInfo = mGroupList.get(groupPosition);
        GroupListAdapter.GroupViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new GroupListAdapter.GroupViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(LAYOUT_RES_GROUP_ID,parent,false);

            viewHolder.mIvTriangle = convertView.findViewById(R.id.iv_triangle);
            viewHolder.mTvGroupName = convertView.findViewById(R.id.tv_group_name);
            viewHolder.mTvChildCount = convertView.findViewById(R.id.tv_child_view_count);

            convertView.setTag(viewHolder);
            //将当前groupPosition设置到tag中，可在长按时获取
            convertView.setTag(LAYOUT_RES_GROUP_ID,groupPosition);
            convertView.setTag(LAYOUT_RES_CHILD_ID,-1);
        }

        viewHolder = (GroupListAdapter.GroupViewHolder) convertView.getTag();
        viewHolder.mIvTriangle.setImageResource(isExpanded ? BG_EXPAND_TRIANGLE_RES_ID : BG_PUT_AWAY_TRIANGLE_RES_ID);
        viewHolder.mTvGroupName.setText(groupInfo.getGroupName());
        viewHolder.mTvChildCount.setText(String.valueOf(groupInfo.getChildInfoList().size()));

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        GroupListAdapter.ChildViewHolder childViewHolder;
        GroupChatInfo groupChatInfo = ((GroupChatInfo) mGroupList.get(groupPosition).getChildInfoList().get(childPosition));
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(LAYOUT_RES_CHILD_ID,parent,false);
            childViewHolder = new GroupListAdapter.ChildViewHolder();
            childViewHolder.mAvatar = convertView.findViewById(R.id.sdv_group_avatar);
            childViewHolder.mTvGroupName = convertView.findViewById(R.id.tv_group_name);
            convertView.setTag(childViewHolder);

            //将groupPosition和childPosition都设置到tag中
            convertView.setTag(LAYOUT_RES_GROUP_ID,groupPosition);
            convertView.setTag(LAYOUT_RES_CHILD_ID,childPosition);
        }else {
            childViewHolder = (GroupListAdapter.ChildViewHolder) convertView.getTag();
        }

        childViewHolder.mAvatar.setImageURI(groupChatInfo.getGroupAvatar());
        childViewHolder.mTvGroupName.setText(groupChatInfo.getGroupName());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static final class GroupViewHolder{
        protected ImageView mIvTriangle;
        protected TextView mTvGroupName;
        protected TextView mTvChildCount;
    }

    private static final class ChildViewHolder {
        protected SimpleDraweeView mAvatar;
        protected TextView mTvGroupName;
    }
}
